package GUI;

	
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

	import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.Books;
import Library.ListOrderBookStudent;

/**
 * @author mishel hen
 *
 */
/**
 * This class for display the search results
 */
public class GUI_SelectBook extends JPanel implements ActionListener {
		/**
		 * Text field for bookId
		 */
		private JTextField bookIdText;
		/**
		 * client Connected
		 */
		private ClientConsole cc;
		/**
		 * panel for search results
		 */
		private GUI_SelectBook sb = this;
		/**
		 * panel for back
		 */
		private JPanel back;
		/**
		 * Button 
		 */
		private ButtonGroup buttonGroup;	
		/**
		 * list for book 
		 */
		private ArrayList<Books> booksList = null;
		/**
		 * 
		 */
		private String command;
		/**
		 * @param clc 
		 * @param books
		 * @param ob
		 */
		public GUI_SelectBook(ClientConsole clc,ArrayList<Books> books, final GUI_OrderBook ob)
		{
			super();	
			cc = clc;
			back = ob;
			booksList = books;
			initialy();
		}
		
		
		public void initialy()
		{
			this.setSize(600, 400);
			
			JLabel lblTitle = new JLabel("Order Book");
			lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));
			lblTitle.setBounds(230, 20, 200, 40);
			add(lblTitle);
			
			JLabel lblTitle1 = new JLabel("Select the book :");
			lblTitle1.setBounds(60, 60, 400, 40);
			add(lblTitle1);
		 
			buttonGroup = new ButtonGroup(); 
			
		    int y = 100;
			for (int i = 0 ; i < booksList.size() ; i++)
			{
				y = y + (i*30);
				addRadio(booksList.get(i).getBookName()+ " by " + booksList.get(i).getAuthor() + " ( "+ booksList.get(i).getEdition()+" )", y); 
				
			}
			
			
			JButton btnorder = new JButton("order");
			btnorder.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					System.out.println(command);
					String book[] = command.split(" ");
					System.out.println(book[0]+book[2]+book[5]);
					for (int i = 0 ; i < booksList.size() ; i++)
					{
						System.out.println(command);
						if ((booksList.get(i).getBookName().equals(book[0])) && (booksList.get(i).getAuthor().equals(book[2]+" "+book[3])) && (booksList.get(i).getEdition().toString().equals(book[5])))
						{
							System.out.println("in if " + book[0]);
							String s[]={"checkOrderBook",booksList.get(i).getBookId().toString(),cc.userName};
							try {
								cc.client.sendToServer(s);
								while(cc.client.flag==0)
								    System.out.print("");
								    cc.client.flag=0;
								    int msgNum =(Integer)cc.client.message;
								    String msg = null;
								    switch (msgNum)
									{
									case 0:	 
										msg = "The book is available in the library";
										break;
									case 1:	
										msg = "There are not copies availabile";
										break;
									case 2:
										msg = "You can't order,your card is not active";
										break;	
									case 3:	
										msg = "The order was done";
										String str[]={"getListOrderBook",cc.userName};
										cc.client.sendToServer(str);
										while(cc.client.flag==0)
											    System.out.print("");
										cc.client.flag=0;
										ListOrderBookStudent listOrderBookStudent =(ListOrderBookStudent)cc.client.message;
										GUI_ShowOrderBook sob= new GUI_ShowOrderBook(cc,listOrderBookStudent.ListOrderBookStudent,back);
										cc.cgui.setContentPane(sob);
										    
										break;
									case 4: // user does not exists
										msg = "There was an error while ordering";
										break;
									}
								JOptionPane.showMessageDialog(new JFrame(),msg);
								
	
							} catch (IOException e) {
								e.printStackTrace();
							}	// send message to server
						}
					}
					
				/*	String s[]={"Order book",text};
					try {
						cc.client.sendToServer(s);
						while(cc.client.flag==0)
						    System.out.print("");
						    cc.client.flag=0;
						    Books books =(Books)cc.client.message;
						    if(books.equals(null))
								JOptionPane.showMessageDialog(new JFrame(),"No books were find");
						    else
							{
								if(books.getNumCopies()<=0)
								{
									JOptionPane.showMessageDialog(new JFrame(),"No copies");
								}
								else
								{
									
								}
								
							}
						    
						    
			
						    	
					} catch (IOException e) {
						e.printStackTrace();
					}	// send message to server

					*/
			}});
			
			JButton btnBack = new JButton("Back");
			
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cc.cgui.setContentPane(back);
				}
			})
			;
			btnBack.setBounds(400, 320, 120, 30);
			add(btnBack);
		setLayout(null);
		
		btnorder.setBounds(60, 320, 120, 30);
		this.add(btnorder);
		this.setVisible(true);	
		

		}


		 private JRadioButton addRadio(String text, int y ) { 
		        JRadioButton b = new JRadioButton(text); 
		        b.addActionListener(this); 
		        buttonGroup.add(b); 
		        b.setBounds(60, y, 200, 20);
		        add(b); 
		        return b; 
		    } 
		 
		 public void actionPerformed(ActionEvent event) { 
		        command = event.getActionCommand(); 
		 }
}
